package com.hongshan.wallet.core.main

import android.app.Application
import android.databinding.ObservableField
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.base.BaseOnPageChangeListener
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.base.SingleLiveEvent
import com.hongshan.wallet.bean.OrderDescBean
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.main
 * @author   : Administrator
 * @data     : 2019/3/19
 * @email    : zhangxx@baihang-china.com
 */
class MainViewModel(application: Application) : MyViewModel(application) {

    val titles = arrayOf(MyApplication.user?.mobile, "交易", "钱包")

    val currentIndex = ObservableField(0)
    val titleText = ObservableField<String>("")

    val onPageChange = BaseOnPageChangeListener {
        titleText.set(titles[it])
    }

    val startPing = SingleLiveEvent<Unit>()
//    val showNotice = SingleLiveEvent<ArrayList<OrderDescBean>>()
    val send = SingleLiveEvent<String>()
    val addAlert = SingleLiveEvent<ArrayList<OrderDescBean>>()

    fun getUserInfo() {
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getUserDesc().execute()
                }
            }
            val body = result.await()?.body()
            if (body?.code == 0) {
                MyApplication.userInfo.set(body.data)
            } else {
                toast(body?.msg)
            }
        }
    }
}